using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BCWSpriteAnimation : MonoBehaviour
{
	[SerializeField]
	private string namePrefix;

	[SerializeField]
	private bool loop;

	private UISprite mySprite;

	[SerializeField]
	private float timeForOneFrame = 0.01f;

	private bool varLoop = true;

	private List<string> spriteName = new List<string>();

	private IEnumerator animationCorotune;

	private void Awake()
	{
		mySprite = GetComponent<UISprite>();
		TakeAllSpriteWithPrefix();
	}

	private void OnEnable()
	{
		if (animationCorotune == null)
		{
			animationCorotune = StartAnimation();
			StartCoroutine(animationCorotune);
		}
	}

	private IEnumerator StartAnimation()
	{
		while (varLoop)
		{
			for (int i = 0; i < spriteName.Count; i++)
			{
				mySprite.spriteName = spriteName[i];
				yield return new WaitForSeconds(timeForOneFrame);
			}
			if (!loop)
			{
				varLoop = false;
			}
		}
	}

	private void OnDisable()
	{
		if (animationCorotune != null)
		{
			StopCoroutine(animationCorotune);
			animationCorotune = null;
		}
	}

	public void TakeAllSpriteWithPrefix()
	{
		spriteName.Clear();
		if (!(mySprite != null) || !(mySprite.atlas != null))
		{
			return;
		}
		List<UISpriteData> spriteList = mySprite.atlas.spriteList;
		for (int i = 0; i < spriteList.Count; i++)
		{
			UISpriteData uISpriteData = spriteList[i];
			if (string.IsNullOrEmpty(namePrefix) || uISpriteData.name.StartsWith(namePrefix))
			{
				spriteName.Add(uISpriteData.name);
			}
		}
		spriteName.Sort();
	}
}
